<?php

/*

* Random Number Game (random.php)

*/

$page_title = "Random Number Game";
$rank_check = 1;
include "header.inc.php";

$game = $_GET["game"];
$act = $_POST["act"];
$code = $_POST["code"];
$guess = $_POST["guess"];

$getcode = fetch("SELECT * FROM randomnumbergame WHERE user = '$userid' AND game = '$game'");
if (!$getcode[code])
{
	mysql_query("INSERT INTO randomnumbergame (user,code,game) VALUES ('$userid','50','$game')");
	$getcode[code] = 50;
}

if ($act != "play")
{
	echo $openHTML;
	echo "<p>I'm thinking of a number between one and a hundred. The closer you guess, the more $pointsVar I'll give you. It only costs 15 $pointsVar to try.</p>";

ECHO <<<END
<P><FORM ACTION="random.php?game=$game" METHOD=POST>
<CENTER><TABLE BGCOLOR="$topAndBottomBG" CELLSPACING=1 CELLPADDING=0 WIDTH=300>
  <TR BGCOLOR="$topAndBottomBG">
	 <TD HEIGHT=50>
		<CENTER><FONT SIZE="+3" COLOR="$topAndBottomText"><B>--Random --
		</B></FONT></CENTER>
	 </TD>
  </TR>
  <TR BGCOLOR="$maincellColor">
	 <TD HEIGHT=90>
		<CENTER><B>My Prediction Is:<BR>
		<INPUT TYPE=text NAME=guess VALUE="" SIZE=9><BR>
		</B><FONT SIZE="-1"><I>(between 1 and
		100)</I></FONT></CENTER>
	 </TD>
  </TR>
  <TR BGCOLOR="$topAndBottomBG">
	 <TD HEIGHT=30>
		<CENTER><FONT SIZE="-1"><INPUT TYPE=submit NAME=Submit VALUE="Guess it!"><INPUT TYPE=hidden NAME=act VALUE=play><INPUT TYPE=hidden NAME=code VALUE=$getcode[code]></FONT></CENTER>
	 </TD>
  </TR>
</TABLE>
</CENTER>
</FORM></P>

END;

	echo $closeHTML;
}

if ($act == "play")
{
	if ($points < 15)
	{
		die(header(error("random.php?game=$game","You don't have enough points to play.")));
	}
	if ($getcode[code] != $code)
	{
		die(header(error("random.php?game=$game","Don't cheat.")));
	}
	$newcode = rand(1,1000);
	mysql_query("UPDATE randomnumbergame SET code = '$newcode' WHERE user = '$userid'");
	if ((($prize) AND ($number)) OR ($prize > 500))
	{
		die(header(error("random.php?game=$game","Don't cheat.")));
	}
	if (($number) OR ($prize))
	{
		mysql_query("UPDATE randomnumbergame SET cheat = '$datestamp' WHERE user = '$userid'");
	}
	if (($prize) AND ($getcode[cheat] == $datestamp))
	{
		$prize = 0;
	}
	if ((!$number) OR ($getcode[cheat] == $datestamp))
	{
		$number = rand(1,100);
	}
	if (($number-25 <= $guess) AND ($guess <= $number+25))
	{
		$prize = 10;
	}
	if (($number-15 <= $guess) AND ($guess <= $number+15))
	{
		$prize = 25;
	}
	if (($number-5 <= $guess) AND ($guess <= $number+5))
	{
		$prize = 50;
	}
	if ($guess == $number)
	{
		$prize = 200;
	}
	if (!$prize)
	{
		$prize = 0;
	}
	points($game,$username,$prize-15);
	echo $openHTML;
	echo "<p>I'm thinking of a number between one and a hundred. The closer you guess, the more $pointsVar I'll give you. It only costs 15 $pointsVar to try.</p>";

ECHO <<<END

<P><FORM ACTION="random.php?game=$game" METHOD=POST>
   <CENTER><TABLE BGCOLOR="$topAndBottomBG" CELLSPACING=1 CELLPADDING=0 WIDTH=300>
      <TR BGCOLOR="$topAndBottomBG">
         <TD HEIGHT=50>
            <CENTER><FONT SIZE="+3" COLOR="$topAndBottomText"><B>--Random --
            </B></FONT></CENTER>
         </TD>
      </TR>
      <TR BGCOLOR="$maincellColor">
         <TD HEIGHT=90>
         <p align=center><font size=-1><i>You guessed $guess. I was thinking of $number. You won $prize $pointsVar.</i></font></p>
            <CENTER><B>My Prediction Is:<BR>
            <INPUT TYPE=text NAME=guess VALUE="" SIZE=9><BR>
            </B><FONT SIZE="-1"><I>(between 1 and
            100)</I></FONT></CENTER>
         </TD>
      </TR>
      <TR BGCOLOR="$topAndBottomBG">
         <TD HEIGHT=30>
            <CENTER><FONT SIZE="-1"><INPUT TYPE=submit NAME=Submit VALUE="Guess it!"><INPUT TYPE=hidden NAME=act VALUE=play><INPUT TYPE=hidden NAME=code VALUE=$newcode></FONT></CENTER>
         </TD>
      </TR>
   </TABLE>
   </CENTER>
</FORM></P>

END;
	echo $closeHTML;
}

?>